extends Marker2D

@onready var overlap_area = $PlayerOverlapArea
@onready var timer = $Timer
@onready var spawn_manager = get_parent()
@export var item_to_spawn: PackedScene = load("res://src/entities/Collectable.tscn")

var spawner_enabled = true
var last_spawned_item = null


func is_player_inside():
	return overlap_area.has_overlapping_bodies()

func spawn_item():
	if spawner_enabled == false:
		print ("oh oh - spawner is disabled right now...")
		return
	spawner_enabled = false
	timer.start()
	
	call_deferred("_add_item_scene")
	

func _on_timer_timeout() -> void:
	spawner_enabled = true

func _add_item_scene():
	var item_instance = item_to_spawn.instantiate()
	add_child(item_instance)
	last_spawned_item = item_instance
	if spawn_manager.has_method("spawn_random"):
		item_instance.item_despawned.connect(Callable(spawn_manager, "add_to_score"))

